import Taro from '@tarojs/taro'

const config = {
  api: "https://www.fastmock.site/mock/e21d61e32c478c779a0df051a452e374/xsrc365/api/v1/",
  noConsole: false,
}

const consoles = v => !config.noConsole && console.log(`${new Date().toLocaleString()}【 M=${v.url} 】P=${JSON.stringify(v.data)}`)
// {
//     url
//     method
//     data
// }
export default async (options = { method: 'GET', data: {} }) => {
  // consoles(options)
  // const authorization = await Taro.storages("authorization")
  return await Taro.request({
    url: config.api + options.url,
    data: options.data,
    // authorization,
    headers: {'Content-Type': 'application/json'},
    method: options.method,
  }).then(({ statusCode, data }) => {
    if (statusCode >= 200 && statusCode < 300) {
      consoles(options)
      if (data.status !== 'ok') {
        Taro.showToast({
          title: `${data.error.message}~` || data.error.code,
          icon: 'none',
          mask: true,
        })
      }
      return data
    } else {
      throw new Error(`网络请求错误，状态码${statusCode}`)
    }
  })
}